﻿using System.ComponentModel;
using System.Linq;
using Loki.Commands;
using Loki.UI;
using Loki.Utils;
using Luna.Model.Configuration;
using Luna.UI.Common;

namespace Luna.UI.Main
{
    public class MainViewModel : ContainerAllActive<object>
    {
        public IConfigurationService Configuration { get; set; }

        public MainViewModel()
        {
            DisplayName = "Luna";
        }

        protected override void OnInitialize()
        {
            Configuration.New();
            base.OnInitialize();
            Commands.Handle(ApplicationCommands.Search, Search_Execute);
            Commands.Handle(LunaCommands.FileOpen, FileOpen_Execute);
            Commands.Handle(LunaCommands.FileSaveAs, FileSaveAs_Execute);
        }

        private void Search_Execute(object sender, CommandEventArgs e)
        {
            SearchActive = !SearchActive;
        }

        private void FileOpen_Execute(object sender, CommandEventArgs e)
        {
            var fileToOpen = UIContext.Windows.GetOpenFileName(new FileDialogInformations() { Filter = "Bases de donnée luna (*.mdf)|*.mdf" });
            if (!string.IsNullOrEmpty(fileToOpen))
            {
                Configuration.Open(fileToOpen);
            }
        }

        private void FileSaveAs_Execute(object sender, CommandEventArgs e)
        {
            var fileToSave = UIContext.Windows.GetSaveFileName(new FileDialogInformations() { Filter = "Bases de donnée luna (*.mdf)|*.mdf", DefaultExtension = ".mdf" });
            if (!string.IsNullOrEmpty(fileToSave))
            {
                Configuration.SaveAs(fileToSave);
            }
        }

        protected override void OnClose()
        {
            Configuration.Close();
            base.OnClose();
        }

        #region SearchActive

        private static PropertyChangedEventArgs argsSearchActiveChanged = ObservableHelper.CreateChangedArgs<MainViewModel>(x => x.SearchActive);

        private static PropertyChangingEventArgs argsSearchActiveChanging = ObservableHelper.CreateChangingArgs<MainViewModel>(x => x.SearchActive);

        private bool searchActive;

        public bool SearchActive
        {
            get
            {
                return searchActive;
            }

            set
            {
                if (value != searchActive)
                {
                    NotifyChanging(argsSearchActiveChanging);
                    searchActive = value;
                    NotifyChanged(argsSearchActiveChanged);
                }
            }
        }

        #endregion SearchActive

        public MenuViewModel Menu
        {
            get { return Items.OfType<MenuViewModel>().FirstOrDefault(); }
            set { EnsureItem(value); }
        }

        public DocumentsViewModel Documents
        {
            get { return Items.OfType<DocumentsViewModel>().FirstOrDefault(); }
            set { EnsureItem(value); }
        }

        public NavigationMenuViewModel Navigation
        {
            get { return Items.OfType<NavigationMenuViewModel>().FirstOrDefault(); }
            set { EnsureItem(value); }
        }
    }
}